<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        ref="trTable"
        isNeedSelection
        isNeedIndex
        :btnList="btnList"
        :total="totalNum"
        v-model="searchParams"
        :searchShowList="searchShowList"
        :searchHideList="searchHideList"
        :tableColumn="tableColumn"
        :minSearchLabelWidth="86"
        requestName="searchHandle"
        :tableData="tableData"
        @add="operateHandle($event, 'add')"
        @print="operateHandle($event, 'print')"
        @remove="operateHandle($event, 'remove')"
        @view="operateHandle($event, 'view')"
        @rowEdit="operateHandle($event, 'rowEdit')"
        @rowPrint="operateHandle($event, 'rowPrint')"
        @rowRemove="operateHandle($event, 'rowRemove')"
        @export="operateHandle($event, 'export')"
      />
    </div>
    <TrEditPopup
      ref="TrEditPopup"
      :width="960"
      v-model="isShowEdit"
      :title="checkTitle"
      :valueObj.sync="formObj"
      :formData="formData"
      :list="popupList"
      :labelWidth="100"
      @select-change="selectChangeHandle"
      @close="closeAddPopup"
      @confrim="editConfirmHandle"
    >
      <template #addTable>
        <TrTable
          ref="trAddTable"
          isPopup
          isNeedSelection
          isNeedIndex
          slotStart
          :btnList="addBtnList"
          :isNeedPagination="false"
          :tableData="addPopuptableData"
          :total="addPopuptotalNum"
          :tableColumn="addPopupTableColumn"
          requestName="getBomListHandle"
          @add="showInnerAddHandle"
          @remove="removeInnerHandle"
        />
      </template>
    </TrEditPopup>
    <TrEditPopup
      ref="TrEditInnerPopup"
      :width="960"
      v-model="isShowInnerEdit"
      title="调拨信息"
      :valueObj.sync="formInnerObj"
      :formData="formInnerData"
      :relationList="relationList"
      :list="popupInnerList"
      @input-change="inputBlurHandle"
      @select-change="innerChangeHandle"
      @close="closeInnerAddPopup"
      @confrim="editInnerConfirmHandle"
    ></TrEditPopup>
    <TrDetail
      v-model="isShowDetailPopup"
      popupTitle="库存调拨单"
      :leftList="detailList"
      :rightList="recordList"
      statusModel="hidden"
      :width="1227"
      :label-width="86"
      :detailData="detailData"
      :isNeedFooter="isNeedFooter"
      :footerList="footerList"
      @reject="rejectHandle"
      @audit="auditHandle"
      @close="closeDetailPopup"
    >
      <template #bomTable>
        <TrTable
          isPopup
          isNeedIndex
          v-model="bomSearchParams"
          :isNeedPagination="false"
          :tableData="bomTableData"
          :tableColumn="bomTableColumn"
          requestName="getBomListHandle"
          @viewDeatil="getDetailTableHandle($event)"
        />
        <TrDetail
          v-model="isShowDetailInnerPopup"
          popupTitle="调拨详情"
          :leftList="detailInnerList"
          statusModel="hidden"
          :width="1227"
          :isNeedFooter="false"
          :detailData="detailInnerData"
          @close="closeDetailInnerPopup"
        >
          <template #bomTable>
            <TrTable
              isPopup
              isNeedIndex
              :isNeedPagination="false"
              :tableData="bomInnerTableData"
              :total="bomInnerTotal"
              :tableColumn="bomInnerTableColumn"
              requestName="getBomListHandle"
              @viewDeatil="operateHandle($event, 'viewDeatil')"
            />
          </template>
        </TrDetail>
      </template>
    </TrDetail>
    <TrEditPopup
      ref="TrEditInnerPopup"
      :width="480"
      v-model="isShowReject"
      title="驳回"
      :valueObj.sync="formRejectObj"
      :formData="formRejectData"
      :list="popupRejectList"
      @close="closeRejectPopup"
      @confrim="rejectConfirmHandle"
    ></TrEditPopup>
    <div class="print-wrap">
      <TransferOrderPrint ref="printRef" :printList="printList" />
    </div>
  </div>
</template>

<script>
import {
  getEnum,
  getNumInfo,
  getList,
  getDetail,
  getRecord,
  getDetailInner,
  add,
  update,
  audit,
  remove,
  reject,
  getPrintData,
} from "@/api/logistics/transferOrder";
import enumData from "@/utils/enumCode.js";
import { getColor } from "@/utils/billColor";
import { getStore } from "@/utils/store";
import TransferOrderPrint from "./components/transferOrderPrint.vue";
import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrEditPopup from "@/components/Tr-edit/Tr-edit-popup.vue";
import TrDetail from "@/components/Tr-detail/Tr-detail.vue";

export default {
  components: {
    TrTable,
    TrEditPopup,
    TrDetail,
    TransferOrderPrint,
  },
  data: () => ({
    tableColumn: [
      {
        type: "text",
        label: "库存调拨单",
        value: "billCode",
      },
      {
        type: "text",
        label: "调拨类型",
        value: "transferTypeLabel",
      },
      {
        type: "text",
        label: "来源仓库",
        value: "transferOutWh",
      },
      {
        type: "text",
        label: "目的仓库",
        value: "transferInWh",
      },
      {
        type: "text",
        label: "调拨申请人",
        value: "transferApplyUserName",
      },
      {
        type: "text",
        label: "单据状态",
        value: "stateLabel",
      },
      {
        type: "text",
        label: "创建人员",
        value: "createUserName",
      },
      {
        type: "text",
        label: "创建时间",
        value: "createTime",
      },
      {
        type: "text",
        label: "修改人员",
        value: "updateUserName",
      },
      {
        type: "text",
        label: "修改时间",
        value: "updateTime",
      },
      {
        type: "option",
        label: "操作",
        fixed: "right",
        width: 120,
        showBtnList: [
          {
            label: "查看",
            option: "view",
          },
        ],
        hideBtnList: [
          {
            label: "编辑",
            option: "rowEdit",
          },
          {
            label: "打印",
            option: "rowPrint",
          },
          {
            label: "删除",
            option: "rowRemove",
          },
        ],
      },
    ],
    searchHideList: [
      {
        id: "billCode",
        type: "input",
        width: 33.33,
        label: "库存调拨单",
        value: "",
      },
      {
        id: "transferOutWhId",
        type: "loadselect",
        width: 33.33,
        label: "来源仓库",
        value: "",
        list: [],
        reviewLabel: "warehouseName",
        requestObj: {
          url: "/api/mes-warehouse-service/warehouse/page",
          method: "post",
          searchKey: "warehouseName",
          rlLabel: "warehouseName",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        id: "transferInWhId",
        type: "loadselect",
        width: 33.33,
        label: "目的仓库",
        value: "",
        list: [],
        reviewLabel: "warehouseName",
        requestObj: {
          url: "/api/mes-warehouse-service/warehouse/page",
          method: "post",
          searchKey: "warehouseName",
          rlLabel: "warehouseName",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        id: "transferApplyUserName",
        type: "input",
        width: 33.33,
        label: "调拨申请人",
        value: "",
      },
      {
        id: "stateList",
        type: "multiSelect",
        width: 33.33,
        label: "状态",
        list: [],
        value: [],
      },

      {
        startStr: "updateTimeStart",
        endStr: "updateTimeEnd",
        id: "updateTime",
        type: "daterange",
        width: 80,
        label: "修改时间",
        value: [],
        timePeriod: true,
        radio: -1,
      },
    ],
    searchShowList: [
      {
        isNeed: true,
        startStr: "createTimeStart",
        endStr: "createTimeEnd",
        id: "createdTime",
        type: "daterange",
        width: 80,
        label: "创建时间",
        value: [],
        timePeriod: true,
        radio: 1,
      },
    ],
    btnList: [
      {
        label: "新增",
        option: "add",
        color: "blue",
      },
      {
        label: "打印",
        option: "print",
        color: "white",
        isRelaTable: true,
      },
      {
        label: "导出",
        option: "export",
        color: "white",
      },
      {
        label: "删除",
        option: "remove",
        color: "white",
        isRelaTable: true,
      },
    ],
    tableData: [],
    printList: [],
    searchParams: {
      current: 1,
      size: 10,
    },
    totalNum: 0,
    // 新增编辑弹窗
    currentHandle: "",
    formObj: {},
    formData: {},
    popupList: [
      {
        label: "调拨申请人",
        type: "loadselect",
        width: 50,
        value: "",
        list: [],
        id: "transferApplyUser",
        selectedLabel: "",
        reviewLabel: "",
        requestObj: {
          url: "/api/trio-user/page",
          method: "get",
          searchKey: "realName",
          rlLabel: "realName/deptName",
          rlValue: "account",
          resultKey: "data,data,records",
        },
      },
      {
        label: "调拨类型",
        type: "select",
        width: 50,
        value: "",
        list: [],
        id: "billTypeCode",
      },
      {
        label: "来源仓库",
        type: "loadselect",
        width: 50,
        value: "",
        list: [],
        id: "transferOutWhId",
        selectedLabel: "",
        reviewLabel: "",
        requestObj: {
          url: "/api/mes-warehouse-service/warehouse-in-user/page",
          method: "post",
          searchKey: "warehouseName",
          rlLabel: "warehouseName",
          rlValue: "warehouseId",
          resultKey: "data,data,records",
        },
        defaultParams: {
          userId: getStore({ name: "userInfo" }).user_id,
        },
      },
      {
        label: "目的仓库",
        type: "loadselect",
        width: 50,
        value: "",
        list: [],
        id: "transferInWhId",
        selectedLabel: "",
        reviewLabel: "",
        requestObj: {
          url: "/api/mes-warehouse-service/warehouse-in-user/page",
          method: "post",
          searchKey: "warehouseName",
          rlLabel: "warehouseName",
          rlValue: "warehouseId",
          resultKey: "data,data,records",
        },
        defaultParams: {
          userId: getStore({ name: "userInfo" }).user_id,
        },
      },
      {
        type: "slot",
        slotName: "addTable",
        width: 100,
        isNeed: false,
      },
    ],
    addPopupTableColumn: [
      {
        type: "text",
        label: "物料料号",
        value: "materialCode",
      },
      {
        type: "text",
        label: "物料名称",
        value: "materialName",
      },
      {
        type: "text",
        label: "规格型号",
        value: "materialSpec",
      },
      {
        type: "text",
        label: "仓库",
        value: "sourceWarehouseName",
      },
      {
        type: "text",
        label: "项目",
        value: "project",
      },
      {
        type: "text",
        label: "数量",
        value: "num",
      },
      {
        type: "text",
        label: "计量单位",
        value: "newNumUnit",
      },
      {
        type: "text",
        label: "重量",
        value: "weight",
      },
      {
        type: "text",
        label: "重量单位",
        value: "newWeightUnit",
      },
      {
        type: "text",
        label: "备注",
        value: "remark",
      },
    ],
    addPopuptableData: [],
    addBtnList: [
      {
        label: "新增",
        option: "add",
        color: "blue",
      },
      {
        label: "删除",
        option: "remove",
        color: "white",
        isRelaTable: true,
      },
    ],
    addPopuptotalNum: 0,
    isShowEdit: false,
    isShowInnerEdit: false,
    availableNum: "",
    materialInfo: {},
    formInnerObj: {},
    formInnerData: {},
    oldInnerList: [],
    popupInnerList: [
      {
        label: "物料料号",
        type: "loadselect",
        width: 50,
        value: "",
        list: [],
        id: "materialId",
        selectedLabel: "",
        requestObj: {
          url: "/api/mes-production-service/material/page",
          method: "post",
          searchKey: "materialCode",
          rlLabel: "materialCode",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        label: "计量单位",
        type: "input",
        width: 50,
        value: "",
        u9Value: "",
        list: [],
        id: "numUnitCode",
        disabled: true,
      },
      {
        label: "物料名称",
        type: "input",
        width: 50,
        value: "",
        id: "materialName",
        disabled: true,
      },
      {
        label: "数量",
        type: "input",
        width: 50,
        value: "",
        id: "num",
      },
      {
        label: "规格型号",
        type: "input",
        width: 50,
        value: "",
        id: "materialSpec",
        disabled: true,
      },
      {
        label: "重量单位",
        type: "input",
        width: 50,
        value: "",
        u9Value: "",
        id: "newWeightUnit",
        disabled: true,
      },
      {
        label: "仓库",
        type: "input",
        width: 50,
        value: "",
        valueId: "",
        id: "sourceWarehouseName",
        disabled: true,
      },
      {
        label: "重量",
        type: "input",
        width: 50,
        value: "",
        id: "weight",
        disabled: true,
      },
      {
        label: "库存数",
        type: "input",
        width: 50,
        value: "",
        id: "warehouseNum",
        disabled: true,
      },
      {
        label: "可用数",
        type: "input",
        width: 50,
        value: "",
        id: "availableNum",
        disabled: true,
      },
      {
        label: "项目",
        type: "select",
        width: 50,
        value: "",
        list: [],
        id: "project",
        isNeed: false,
      },
      {
        label: "备注",
        type: "textarea",
        width: 100,
        value: "",
        id: "remark",
        isNeed: false,
      },
    ],
    relationList: [
      {
        id: "materialId",
        relaList: [
          {
            relaId: "materialName",
            relaValue: "materialName",
            reviewValue: "materialName",
            disabled: true,
          },
          {
            relaId: "num",
            // disabled: true,
          },
          {
            relaId: "materialSpec",
            relaValue: "materialSpec",
            reviewValue: "materialSpec",
            disabled: true,
          },
          // {
          //   relaId: "numUnitCode",
          //   relaValue: "unitCode",
          //   reviewValue: "unitCode",
          //   disabled: true,
          // },
        ],
      },
    ],
    //详情弹窗
    detailData: {},
    isShowDetailPopup: false,
    detailList: [
      {
        type: "text",
        label: "基础信息",
        list: [
          {
            width: 50,
            text: "库存调拨单",
            id: "billCode",
          },
          {
            width: 50,
            text: "调拨申请人",
            id: "transferApplyUserName",
          },
          {
            width: 50,
            text: "调拨类型",
            id: "transferTypeLabel",
          },
          {
            width: 50,
            text: "来源仓库",
            id: "transferOutWh",
          },
          {
            width: 50,
            text: "目的仓库",
            id: "transferInWh",
          },
          {
            width: 50,
            text: "驳回原因",
            id: "rejectReason",
            hide: true,
          },
        ],
      },
      {
        type: "slot",
        slotName: "bomTable",
        label: "库存调拨信息",
        slotBottom: 32,
      },
    ],
    recordList: [
      {
        label: "审核记录",
        type: "record",
        id: "record",
        list: [],
      },
    ],
    isNeedFooter: false,
    footerList: [
      {
        label: "驳回",
        option: "reject",
        color: "white",
      },
      {
        label: "审核",
        option: "audit",
        color: "blue",
      },
    ],
    currentDetail: "",
    bomSearchParams: {
      current: 1,
      size: 10,
    },
    bomTotal: 0,
    bomTableColumnOne: [
      {
        type: "text",
        label: "物料料号",
        value: "materialCode",
      },
      {
        type: "text",
        label: "物料名称",
        value: "materialName",
      },
      {
        type: "text",
        label: "规格型号",
        value: "materialSpec",
      },
      {
        type: "text",
        label: "仓库",
        value: "sourceWarehouseName",
      },
      {
        type: "text",
        label: "项目",
        value: "project",
      },
      {
        type: "text",
        label: "数量",
        value: "num",
      },
      {
        type: "text",
        label: "数量单位",
        value: "newNumUnit",
      },
      {
        type: "text",
        label: "重量",
        value: "weight",
      },
      {
        type: "text",
        label: "重量单位",
        value: "newWeightUnit",
      },
      {
        type: "text",
        label: "备注",
        value: "remark",
      },
    ],
    bomTableColumnTwo: [
      {
        type: "text",
        label: "物料料号",
        value: "materialCode",
      },
      {
        type: "text",
        label: "物料名称",
        value: "materialName",
      },
      {
        type: "text",
        label: "规格型号",
        value: "materialSpec",
      },
      {
        type: "text",
        label: "仓库",
        value: "sourceWarehouseName",
      },
      {
        type: "text",
        label: "项目",
        value: "project",
      },
      {
        type: "text",
        label: "数量",
        value: "num",
      },
      {
        type: "text",
        label: "数量单位",
        value: "newNumUnit",
      },
      {
        type: "text",
        label: "重量",
        value: "weight",
      },
      {
        type: "text",
        label: "重量单位",
        value: "newWeightUnit",
      },
      {
        type: "text",
        label: "备注",
        value: "remark",
      },
      {
        type: "option",
        label: "操作",
        fixed: "right",
        showBtnList: [
          {
            label: "查看",
            option: "viewDeatil",
          },
        ],
      },
    ],
    bomTableData: [],
    detailInnerData: {},
    isShowDetailInnerPopup: false,
    detailInnerList: [
      {
        type: "text",
        label: "基础信息",
        list: [
          {
            width: 33.33,
            text: "物料料号",
            id: "materialCode",
          },
          {
            width: 33.33,
            text: "备料数量",
            id: "materialNum",
          },
          {
            width: 33.33,
            text: "物料名称",
            id: "materialName",
          },
          {
            width: 33.33,
            text: "仓库",
            id: "sourceWarehouseName",
          },
          {
            width: 33.33,
            text: "规格型号",
            id: "materialSpec",
          },
        ],
      },
      {
        type: "slot",
        slotName: "bomTable",
        label: "物料标签",
        slotBottom: 32,
      },
    ],
    bomInnerTotal: 0,
    bomInnerTableColumn: [
      {
        type: "text",
        label: "物料标签",
        value: "barcode",
      },
      {
        type: "text",
        label: "数量",
        value: "materialNum",
      },
      {
        type: "text",
        label: "重量",
        value: "materialWeight",
      },
    ],
    bomInnerTableData: [],
    beforeObject: {},
    isShowReject: false,
    formRejectObj: {},
    formRejectData: {},
    popupRejectList: [
      {
        label: "驳回理由",
        type: "textarea",
        width: 100,
        value: "",
        id: "rejectReason",
      },
    ],
  }),
  computed: {
    checkTitle() {
      const titles = {
        add: "新增",
        default: "编辑",
      };
      return titles[this.currentHandle] || titles.default;
    },
    ids() {
      let ids = [];
      this.$refs.trTable.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
    popupIds() {
      let ids = [];
      this.$refs.trAddTable.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
    bomTableColumn() {
      return this.currentDetail
        ? this.bomTableColumnOne
        : this.bomTableColumnTwo;
    },
  },
  mounted() {
    this.getEnmuData();
    // this.getListHandle();
    this.getDictData();
  },
  methods: {
    async operateHandle(val, type) {
      this.currentHandle = type;
      this.$validateData(val, 3);
      const actions = {
        add: () => {
          this.formData = {};
          this.isShowEdit = true;
        },
        print: () => this.printHandle(this.ids),
        export: () =>
          this.$exportHandle({ vm: this, params: this.searchParams }),
        remove: () => this.removeHandle(this.ids),
        view: () => this.getDetailHandle(val),
        rowEdit: () => this.editHandle(val),
        rowPrint: () => this.printHandle([val.id]),
        rowRemove: () => this.removeHandle([val.id]),
      };

      const action = actions[type];
      if (action) action();
    },
    inputBlurHandle(item) {
      if (item.id != "num") return;
      if (item.value == "") return;
      let materialInfo = this.materialInfo;
      if (item.value > this.availableNum || item.value <= 0) {
        this.$refs.TrEditInnerPopup.outReset("weight", "");
        return this.$message.warning("数量不能小于0，且不能超过可用数");
      }

      if (
        materialInfo.standardWeight == 0 &&
        materialInfo.standardWeightUnitCode == materialInfo.unitCode
      ) {
        materialInfo.standardWeight = 1;
      }

      let computedWeight = Number(
        item.value * materialInfo.standardWeight
      ).toFixed(6);
      this.$refs.TrEditInnerPopup.outReset("weight", computedWeight);
    },
    selectChangeHandle(item) {
      if (!Object.keys(this.beforeObject).length) {
        this.beforeObject = item;
      }
      if (item.ftId == "transferOutWhId") {
        let warehouseName = this.$findByProperty(
          this.popupInnerList,
          "id",
          "sourceWarehouseName"
        );
        this.$set(warehouseName, "value", item.warehouseName);
        this.$set(warehouseName, "valueId", item.rlValue);
        this.formObj.transferOutWh = item.warehouseName;
        if (this.addPopuptableData.length != 0) {
          this.$textShow({
            title: "删除",
            content: [this.$t("tip.deleteTransfer")],
          })
            .then(() => {
              this.addPopuptableData = [];
            })
            .catch(() => {
              let transferOutWh = this.$findByProperty(
                this.popupList,
                "id",
                "transferOutWhId"
              );
              this.$set(transferOutWh, "value", this.beforeObject.rlValue);
              this.$set(
                transferOutWh,
                "selectedLabel",
                this.beforeObject.rlLabel
              );
              this.$refs.TrEditPopup.outReset("transferOutWhId", transferOutWh);
              this.$set(warehouseName, "value", this.beforeObject.rlLabel);
              this.$set(warehouseName, "valueId", this.beforeObject.rlValue);
            });
        }
      }
      if (item.ftId == "transferInWhId") {
        this.formObj.transferInWh = item.warehouseName;
      }
      if (item.ftId == "transferApplyUser") {
        this.formObj.transferApplyUserName = item.realName;
      }
    },
    async innerChangeHandle(item) {
      if (item.ftId == "materialId") {
        this.materialInfo = item;
        let warehouseName = this.$findByProperty(
          this.popupInnerList,
          "id",
          "sourceWarehouseName"
        );
        let numUnitData = this.$findByProperty(
          this.popupInnerList,
          "id",
          "numUnitCode"
        );
        let newWeight = this.$findByProperty(
          this.popupInnerList,
          "id",
          "newWeightUnit"
        );
        this.$set(numUnitData, "value", `${item.unitCode}(${item.unitName})`);
        this.$set(numUnitData, "u9Value", item.unitU9Code);
        this.$set(
          newWeight,
          "value",
          `${item.standardWeightUnitCode}(${item.standardWeightUnitName})`
        );
        this.$set(newWeight, "u9Value", item.standardWeightU9UnitCode);
        this.$refs.TrEditInnerPopup.outReset("numUnitCode", numUnitData);
        this.$refs.TrEditInnerPopup.outReset("newWeightUnit", newWeight);
        this.$refs.TrEditInnerPopup.outReset("weight", "");
        let params = {
          bizBillId: this.currentHandle == "rowEdit" ? this.formData.id : null,
          materialId: item.rlValue,
          warehouseId: warehouseName.valueId,
        };
        await this.getNumberHandle(params);
      }
    },
    changeSelectHandle(val) {
      if (val.ftId == "workOrderId") {
        this.getAddTableData(val.workOrderCode);
      }
    },
    getEnmuData() {
      getEnum(["transferOrderStateEnum"]).then((res) => {
        if (res.data.code != 200) return;
        let state = this.$findByProperty(
          this.searchHideList,
          "id",
          "stateList"
        );
        this.$set(state, "list", res.data.data[0].list);
      });
    },
    async getNumberHandle(params) {
      const { data } = await getNumInfo(params);
      if (data.code != 200) return;
      this.availableNum = data.data.availableNum ? data.data.availableNum : 0;
      this.$refs.TrEditInnerPopup.outReset(
        "warehouseNum",
        data.data.warehouseNum
      );
      this.$refs.TrEditInnerPopup.outReset(
        "availableNum",
        data.data.availableNum
      );
    },
    async editHandle(val) {
      if (val.state != 10)
        return this.$message.warning("只允许编辑待调拨状态的单据！");
      const { data } = await getDetail(val.id);
      if (data.code !== 200) return;
      this.formData = data.data;
      const {
        transferDetailList,
        transferApplyUser,
        transferApplyUserName,
        transferOutWhId,
        transferOutWh,
        transferInWhId,
        transferInWh,
      } = data.data;
      this.addPopuptableData = transferDetailList.map((item) => {
        return {
          ...item,
          warehouseName: item.sourceWarehouseName,
          newNumUnit: `${item.numUnitCode}(${item.numUnitName})`,
          newWeightUnit: `${item.weightUnitCode}(${item.weightUnitName})`,
          numUnitU9Code: item.numUnitU9Code,
          weightUnitU9Code: item.weightUnitU9Code,
        };
      });
      this.oldInnerList = transferDetailList;
      let transferApplyUserPopup = this.$findByProperty(
        this.popupList,
        "id",
        "transferApplyUser"
      );
      this.formObj.transferApplyUserName = transferApplyUserName;
      this.$set(transferApplyUserPopup, "value", transferApplyUser);
      this.$set(transferApplyUserPopup, "selectedLabel", transferApplyUserName);
      let transferOutWhIdPopup = this.$findByProperty(
        this.popupList,
        "id",
        "transferOutWhId"
      );
      this.formObj.transferOutWh = transferOutWh;
      this.$set(transferOutWhIdPopup, "value", transferOutWhId);
      this.$set(transferOutWhIdPopup, "selectedLabel", transferOutWh);
      let transferInWhIdPopup = this.$findByProperty(
        this.popupList,
        "id",
        "transferInWhId"
      );
      this.formObj.transferInWh = transferInWh;
      this.$set(transferInWhIdPopup, "value", transferInWhId);
      this.$set(transferInWhIdPopup, "selectedLabel", transferInWh);
      this.beforeObject.rlLabel = transferOutWh;
      this.beforeObject.rlValue = transferOutWhId;
      let warehouseName = this.$findByProperty(
        this.popupInnerList,
        "id",
        "sourceWarehouseName"
      );
      this.$set(warehouseName, "value", transferOutWh);
      this.$set(warehouseName, "valueId", transferOutWhId);

      this.isShowEdit = true;
    },
    showInnerAddHandle() {
      if (!Object.keys(this.beforeObject).length) {
        return this.$message.warning("请先选择来源仓库");
      }
      this.isShowInnerEdit = true;
    },
    removeInnerHandle() {
      this.$textShow({
        title: this.$t("tip.label"),
        content: [this.$t("tip.delete")],
      }).then(() => {
        let tempObj = this.addPopuptableData.filter(
          (item) => !this.popupIds.includes(item.id)
        );
        this.addPopuptableData = tempObj;
        this.$refs.trAddTable.clearSelect();
      });
    },
    editConfirmHandle() {
      if (this.addPopuptableData.length == 0)
        return this.$message.warning("调拨信息不能为空！");
      if (this.formObj.transferOutWhId == this.formObj.transferInWhId)
        return this.$message.warning("来源仓库不能与目的仓库相同！");
      let params = {
        ...this.formObj,
        id: this.formData.id,
        transferOrderDetailList: this.addPopuptableData,
      };
      if (this.currentHandle == "rowEdit") {
        params.transferOrderDetailDelIds = this.oldInnerList.map(
          (item) => item.id
        );
        this.updateHandle(params);
      } else {
        this.addHandle(params);
      }
    },
    editInnerConfirmHandle() {
      let warehouseName = this.$findByProperty(
        this.popupInnerList,
        "id",
        "sourceWarehouseName"
      );
      let numUnitData = this.$findByProperty(
        this.popupInnerList,
        "id",
        "numUnitCode"
      );
      let newWeight = this.$findByProperty(
        this.popupInnerList,
        "id",
        "newWeightUnit"
      );
      this.addPopuptableData.push({
        id: Number(this.addPopuptableData.length) + 1,
        ...this.formInnerObj,
        sourceWarehouseId: warehouseName.valueId,
        materialCode: this.materialInfo.materialCode,
        numUnitU9Code: numUnitData.u9Value,
        weightUnitU9Code: newWeight.u9Value,
        newNumUnit: numUnitData.value,
        newWeightUnit: newWeight.value,
      });
      this.isShowInnerEdit = false;
      this.$nextTick(() => {
        this.initListHandle();
      });
    },
    async rejectConfirmHandle() {
      let { data } = await reject(this.formRejectObj);
      if (data.code != 200) return;
      this.$message.success(this.$t("msg.success"));
      this.getListHandle();
      this.isShowReject = false;
    },
    async getDictData() {
      const { ["PC-TRANSFER-PROJECT"]: transferProject } =
        await this.$fetchEnumData([enumData.transferProject]);
      let project = this.$findByProperty(this.popupInnerList, "id", "project");
      this.$set(project, "list", transferProject);
      const { ["PC-TRANSFER-ORDER-TYPE"]: transferOrderTypeList } =
        await this.$fetchEnumData([enumData.transferOrderTypeList]);
      let billTypeCode = this.$findByProperty(
        this.popupList,
        "id",
        "billTypeCode"
      );
      this.$set(billTypeCode, "list", transferOrderTypeList);
    },
    async printHandle(ids) {
      this.printList = [];
      getPrintData({
        billIdList: ids,
      }).then(({ data }) => {
        if (data.code !== 200) return;

        this.$validateData(data.data);
        this.printList = data.data;
      });
      await this.$textShow({
        title: "打印",
        content: [this.$t("tip.print")],
      });
      this.$refs.printRef.printHandle();
      this.$refs.trTable.clearSelect();
    },
    removeHandle(billIdList) {
      this.$textShow({
        title: this.$t("tip.label"),
        content: [this.$t("tip.delete")],
      })
        .then(async () => {
          let { data } = await remove({ billIdList });
          if (data.code != 200) return;
          this.$message.success(this.$t("msg.success"));
          this.$refs.trTable.clearSelect();
          this.getListHandle();
        })
        .catch(() => {});
    },
    async addHandle(params) {
      let res = await add(params);

      if (res.status != 200) return;
      this.$message.success(this.$t("msg.success"));
      await this.getListHandle();
      this.closeAddPopup();
    },
    async updateHandle(params) {
      let res = await update(params);

      if (res.status != 200) return;
      this.$message.success(this.$t("msg.success"));
      await this.getListHandle();
      this.closeAddPopup();
    },
    rejectHandle() {
      this.formRejectObj.billId = this.detailData.id;
      this.isShowReject = true;
    },
    async auditHandle() {
      await this.$textShow({
        title: this.$t("tip.auditLabel"),
        content: [this.$t("tip.audit")],
      });
      let res = await audit(this.detailData.id);
      if (res.status != 200) return;
      this.$message.success(this.$t("msg.success"));
      await this.getListHandle();
    },
    async getDetailHandle(val) {
      const { data } = await getDetail(val.id);
      if (data.code !== 200) return;
      // 是否需要显示驳回原因
      let flag = !(val.state == 20 && data.data.rejectReason != "");
      this.$validateData(data.data);
      //详情表格是否需要查看按钮
      this.currentDetail = val.state == 10 || val.state == 20 ? true : false;
      // 是否需要底部详情按钮
      this.isNeedFooter = val.state == 30 ? true : false;
      this.detailData = data.data;
      this.bomTableData = data.data.transferDetailList;
      this.bomTableData.forEach((item) => {
        item.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
        item.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
      });
      let isShowReason = this.$findByProperty(
        this.detailList[0].list,
        "id",
        "rejectReason"
      );
      this.$set(isShowReason, "hide", flag);

      const { data: record } = await getRecord(val.id);
      const recordCol = this.$findByProperty(this.recordList, "id", "record");
      recordCol.list = record.data.map((item) => {
        return {
          reTime: item.createTime,
          option: item.operTypeName,
          iconClass: item.operType == 2 ? "blue-warn" : undefined,
          labelList: [
            {
              label: "审核人员",
              value: item.createUserName,
            },
          ],
          remarkList:
            item.operType == 10
              ? [
                  {
                    label: "驳回理由",
                    value: item.remark,
                  },
                ]
              : [],
        };
      });

      this.isShowDetailPopup = true;
    },
    async getDetailTableHandle(val) {
      const { data } = await getDetailInner({
        bizDetailId: val.id,
      });

      if (data.code !== 200) return;
      this.$validateData(data.data);
      this.isShowDetailInnerPopup = true;
      this.detailInnerData = {
        ...data.data,
        materialNum: `${data.data.num}${data.data.numUnitCode}`,
      };
      this.bomInnerTableData = data.data.barcodeDetailRespList;
      this.bomInnerTableData.forEach((item) => {
        item.materialNum = `${item.num}${item.numUnitCode}`;
        item.materialWeight = `${item.weight}${item.weightUnitCode}`;
      });
      this.bomTotal = Number(data.data.total);
    },
    closeAddPopup() {
      let transferApplyUserPopup = this.$findByProperty(
        this.popupList,
        "id",
        "transferApplyUser"
      );
      this.$set(transferApplyUserPopup, "value", "");
      let transferOutWhIdPopup = this.$findByProperty(
        this.popupList,
        "id",
        "transferOutWhId"
      );
      this.$set(transferOutWhIdPopup, "value", "");
      let transferInWhIdPopup = this.$findByProperty(
        this.popupList,
        "id",
        "transferInWhId"
      );
      this.$set(transferInWhIdPopup, "value", "");
      this.initListHandle();
      this.addPopuptableData = [];
      this.beforeObject = {};
      this.isShowEdit = false;
    },
    initListHandle() {
      this.popupInnerList.forEach((item) => {
        item.value = "";
        if (item.u9Value) item.u9Value = "";
        if (item.selectedLabel) item.selectedLabel = "";
      });
    },
    closeInnerAddPopup() {
      this.isShowInnerEdit = false;
    },
    closeDetailPopup() {
      this.isShowDetailPopup = false;
    },
    closeRejectPopup() {
      this.isShowReject = false;
    },
    closeDetailInnerPopup() {
      this.isShowDetailInnerPopup = false;
    },
    searchHandle() {
      this.getListHandle();
    },
    async getListHandle() {
      const { data } = await getList(this.searchParams);
      if (data.code != 200) return;

      this.$validateData(data.data);
      this.tableData = data.data.records;
      this.totalNum = Number(data.data.total);
      this.tableData.forEach((item) => {
        item["stateLabelColor"] = getColor("DBD", item.state);
        item["isShowrowEdit"] = item.state != 10;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.print-wrap {
  position: fixed;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: -999;
  opacity: 0;
}
</style>
